package com.aooci.ordercat.jsf.mp;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.aooci.ordercat.domain.Ordercat;
import com.aooci.ordercat.domain.Recette;
import com.aooci.ordercat.enums.OrderStatus;
import com.aooci.ordercat.service.AccountingService;
import com.aooci.ordercat.service.SystemParameter;
import com.aooci.ordercat.vo.RecetteColumnsVo;
import com.aooci.ordercat.vo.RecetteSummaryVo;

//@ManagedBean
@RequestScoped
@Component
public class AccountingController implements Serializable{

	private static final long serialVersionUID = -2625685756120786130L;

	private static final Logger log = Logger.getLogger(AccountingController.class);
	

	@Autowired
	@Qualifier("accountingService")
	private transient AccountingService accountingService;
	
	private Ordercat[] selectedOrdercats;
    private Date operatedDate = new Date();
    private OrderStatus operatedStatus = OrderStatus.PAID;
    private int operatedDivideTime = SystemParameter.RECETTE_DIVIDE_TIME;
    private RecetteSummaryVo recetteSummaryVo;
    private RecetteColumnsVo recetteColumnsVo = new RecetteColumnsVo();
    private List<Recette> recettes;
    
    
	public void changeOrdercatStatusListener(ActionEvent event){
		OrderStatus newStatus = OrderStatus.valueOf((String) event.getComponent().getAttributes().get("newStatus"));
		this.accountingService.changeOrdercatStatus(this.selectedOrdercats, newStatus);
	}
	
	public void displayRecetteListener(ActionEvent event){
		this.recettes = this.accountingService.loadRecettesBySpecifiedDate(this.operatedDate, this.operatedDivideTime);
		this.recetteSummaryVo = this.accountingService.calculRecetteSummary(this.recettes);
	}
	
	public void deleteOrdercatListener(ActionEvent event){
		this.accountingService.deleteOrdercats(this.selectedOrdercats);
	}

    /**
	 * @return the operatedDate
	 */
	public Date getOperatedDate() {
		return operatedDate;
	}

	/**
	 * @param operatedDate the operatedDate to set
	 */
	public void setOperatedDate(Date operatedDate) {
		this.operatedDate = operatedDate;
	}

    public List<Ordercat> getOrdercats(){
    	return this.accountingService.loadOrdercatsBySpecifiedDate(this.operatedDate, this.operatedStatus, this.operatedDivideTime);
    }
    
    public List<Recette> getRecettes() {
    	return this.recettes;
    }

	/**
	 * @return the operatedStatus
	 */
	public OrderStatus getOperatedStatus() {
		return operatedStatus;
	}

	/**
	 * @param operatedStatus the operatedStatus to set
	 */
	public void setOperatedStatus(OrderStatus operatedStatus) {
		this.operatedStatus = operatedStatus;
	}

	/**
	 * @return the recetteSummaryVo
	 */
	public RecetteSummaryVo getRecetteSummaryVo() {
		return recetteSummaryVo;
	}



	/**
	 * @return the recetteColumnsVo
	 */
	public RecetteColumnsVo getRecetteColumnsVo() {
		return recetteColumnsVo;
	}



	/**
	 * @param recetteColumnsVo the recetteColumnsVo to set
	 */
	public void setRecetteColumnsVo(RecetteColumnsVo recetteColumnsVo) {
		this.recetteColumnsVo = recetteColumnsVo;
	}

	/**
	 * @return the operatedDivideTime
	 */
	public int getOperatedDivideTime() {
		return operatedDivideTime;
	}

	/**
	 * @param operatedDivideTime the operatedDivideTime to set
	 */
	public void setOperatedDivideTime(int operatedDivideTime) {
		this.operatedDivideTime = operatedDivideTime;
	}

	/**
	 * @return the selectedOrdercats
	 */
	public Ordercat[] getSelectedOrdercats() {
		return selectedOrdercats;
	}

	/**
	 * @param selectedOrdercats the selectedOrdercats to set
	 */
	public void setSelectedOrdercats(Ordercat[] selectedOrdercats) {
		this.selectedOrdercats = selectedOrdercats;
	}
	
	
}
